import jieba
import numpy as np
import pandas as pd

# 把天气相关的语料称为正语料，其他类别的语料称为负语料
pos = pd.read_csv('./data/weather_pos.txt', encoding='UTF-8', header=None)
neg = pd.read_csv('./data/weather_neg.txt', encoding='UTF-8', header=None)

# 用jieba对每一条语料进行分词处理
pos['words'] = pos[0].apply(lambda x: jieba.lcut(x))
neg['words'] = neg[0].apply(lambda x: jieba.lcut(x))

#正负语料进行合并成训练语料然后并打上标签，正语料打上标签1，负语料打上标签0
x = np.concatenate((pos['words'], neg['words']))
y = np.concatenate((np.ones(len(pos)),np.zeros(len(neg))))

# 训练词向量
from gensim.models.word2vec import Word2Vec
word2vec = Word2Vec(x, size=300, window=3, min_count=5, sg=1, hs=1, iter=10, workers=25)
word2vec.save('./data/word2vec.model')

# 获取整句话的词向量
def total_vector(words):
    vec = np.zeros(300).reshape((1, 300))
    for word in words:
        try:
            vec += word2vec.wv[word].reshape((1, 300))
        except KeyError:
            continue
    return vec

# 得到训练集
train_vec = np.concatenate([total_vector(words) for words in x])


# 训练分类模型
from sklearn.externals import joblib
from sklearn.model_selection import cross_val_score
from sklearn.svm import SVC

model = SVC(kernel='rbf', verbose=True)
model.fit(train_vec, y)
joblib.dump(model, '../Desktop/weather_svm.pkl')



# 预测
def svm_predict(query):
    words = jieba.lcut(str(query))
    words_vec = total_vector(words)
    result = model.predict(words_vec)
    if int(result) == 1:
        print('类别：天气')
    elif int(result) == 0:
        print('类别：其他')